﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QSocket.Core
{
    public class ServerBase : IDisposable
    {
        public ServerBase()
        {
            Application = new ApplicationBase();
            Application.Parame = new System.Collections.Hashtable();
            ClearDay = 2;
        }
        

        /// <summary>
        /// 锁字符
        /// </summary>
        protected const string LOCK = "LOCK";

        private bool m_Run = false;

        /// <summary>
        /// 是否运行
        /// </summary>
        public bool Run 
        {
            get
            {
                return m_Run;
            }
            protected set
            {
                if (value == true)
                {
                    this.ServerRuning();
                }
                else if (value == false)
                {
                    this.ServerStoping();
                }

                m_Run = value;

                if (value == true)
                {
                    this.ServerRun();
                }
                else if(value == false)
                {
                    this.ServerStop();
                }
            }
        }
        
        /// <summary>
        /// 每次间隔多少天进行一次垃圾回收
        /// </summary>
        protected int ClearDay { get; set; }
        
        /// <summary>
        /// 客户端Session集合
        /// </summary>
        public virtual List<SessionBase> Sessions { get { return null; } }
        
        /// <summary>
        /// 应用程序
        /// </summary>
        public ApplicationBase Application { get; set; }
        
        /// <summary>
        /// 当有客户进行连接时触发的事件
        /// </summary>
        public event AcceptClientEventHandle OnAcceptClient;
        
        /// <summary>
        /// 当有客户下线时触发的事件
        /// </summary>
        public event ClientDownlineEventHandle OnClientDownline;
        
        /// <summary>
        /// 当有用户发送数据时触发的事件
        /// </summary>
        public event ReceiveMessageEventHandle OnReceiveMessages;

        /// <summary>
        /// 当服务器启动之前触发的事件
        /// </summary>
        public event EventHandler OnServerRuning;

        /// <summary>
        /// 当服务器启动后触发的事件
        /// </summary>
        public event EventHandler OnServerRun;

        /// <summary>
        /// 当服务器停止之前触发的事件
        /// </summary>
        public event EventHandler OnServerStoping;
        
        /// <summary>
        /// 当服务器停止后触发的事件
        /// </summary>
        public event EventHandler OnServerStop;

        /// <summary>
        /// 运行服务器
        /// </summary>
        public virtual void RunServer() 
        {
            Run = true;
        }

        /// <summary>
        /// 重新启动服务器
        /// </summary>
        public virtual void ResetServer()
        {
            if (this.Run == true)
            {
                this.StopServer();
            }
            this.RunServer();
        }

        /// <summary>
        /// 停止服务器
        /// </summary>
        public virtual void StopServer() 
        {
            Run = false;
        }

        protected virtual void MessageBroadcast(byte[] msg)
        {

        }

        protected virtual void AcceptClientHandler(SessionBase session)
        {
            if (OnAcceptClient != null)
                OnAcceptClient(session);
        }

        protected virtual void ClientDownlineHandler(SessionBase session)
        {
            if (OnClientDownline != null)
                OnClientDownline(session);
        }

        protected virtual void ReceiveMessageHandler(SessionBase session, List<byte> msg)
        {
            if (OnReceiveMessages != null)
                OnReceiveMessages(session, msg);
        }

        public void ServerRuning()
        {
            if (OnServerRuning != null)
                OnServerRuning(this, null);
        }

        public void ServerRun()
        {
            if (OnServerRun != null)
                OnServerRun(this, null);
        }

        public void ServerStoping()
        {
            if (OnServerStoping != null)
                OnServerStoping(this, null);
        }

        public void ServerStop()
        {
            if (OnServerStop != null)
                OnServerStop(this, null);
        }

        public virtual void Dispose()
        {
        }
    }
}
